package com.ruoyi.station.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.station.domain.SCondition;
import com.ruoyi.station.mapper.SConditionMapper;
import com.ruoyi.station.service.SConditionService;
import com.ruoyi.system.mapper.SysDictDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 追加条件Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-15
 */
@Slf4j
@Service
public class SConditionServiceImpl extends ServiceImpl<SConditionMapper, SCondition> implements SConditionService {
    @Autowired
    private SConditionMapper sConditionMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<Map> getListByModel(String model) {
        List<SCondition> sConditions = sConditionMapper.selectList(new QueryWrapper<SCondition>().eq("model",model));
        List<Map> resultMap = new ArrayList<>();
        for (SCondition sCondition : sConditions) {
            Map map = new HashMap();
            map.put("dictLabel",sCondition.getFieldName());
            map.put("dictValue",sCondition.getFieldCode());
            map.put("conditon",parseCondition(sCondition.getFieldCondition()));
            if ("select".equals(sCondition.getType())){
                map.put("type","select");
                SysDictData sysDictData = new SysDictData();
                sysDictData.setDictType(sCondition.getDictType());
                List<SysDictData> dictDataList = sysDictDataMapper.selectDictDataList(sysDictData);
                map.put("selectDict",dictDataList);
            }else {
                map.put("type",sCondition.getType());
            }
            resultMap.add(map);
        }
        return resultMap;
    }

    public List<Map> parseCondition(String conditionStr){
        String[] split = conditionStr.split(";");
        List<Map> mapList = new ArrayList<>();
        for (String s1 : split) {
            Map map = new HashMap();
            map.put("dictValue",s1.split(":")[0]);
            map.put("dictLabel",s1.split(":")[1]);
            mapList.add(map);
        }
        return mapList;
    }




}
